package com.pkk.components.util.email.send;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.annotation.Resource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.pkk.components.common.constant.SysUtilConstant;
import com.pkk.components.util.email.config.BaseEmailConfig;
import com.pkk.components.util.email.model.EmailProperties;

import lombok.extern.slf4j.Slf4j;


/**
 * @description:
 * @author: peikunkun
 * @create: 2018-09-08 15:17
 **/
@Slf4j
public class SendMailUtil {


  /**
   * 邮件发送的方法
   *
   * @param to 多个收件人
   * @param subject 主题
   * @param content 内容
   * @param smtp 协议
   * @param host 发送服务器服务器
   * @param sendName 邮件发送人
   * @param sendPort 邮件发送人端口
   * @param userName 邮件发送人名
   * @param userPwd 邮件发送人密码 自己测试时用的是授权码不是密码
   * @return 成功或失败
   */
  private static boolean send(String[] to, String subject, Object content, String smtp, String host,
      String sendName, String sendPort, String userName, String userPwd) {

    // 第一步：创建Session
    Properties props = new Properties();
    // 指定邮件的传输协议，smtp(Simple Mail Transfer Protocol 简单的邮件传输协议)
    props.put("mail.transport.protocol", smtp);
    // 指定邮件发送服务器服务器 "smtp.qq.com"
    props.put("mail.host", host);
    // 指定邮件的发送人(您用来发送邮件的服务器，比如您的163\sina等邮箱)
    props.put("mail.from", sendName);
    if (true) {
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.socketFactory.fallback", "false");
      props.put("mail.smtp.socketFactory.port", sendPort);
    }
    Session session = Session.getDefaultInstance(props);

    // 开启调试模式
    session.setDebug(true);
    try {
      // 第二步：获取邮件发送对象
      Transport transport = session.getTransport();
      // 连接邮件服务器，链接您的163、sina邮箱，用户名（不带@163.com，登录邮箱的邮箱账号，不是邮箱地址）、密码
      transport.connect(userName, userPwd);

      // 第三步：创建邮件消息体
      MimeMessage message = new MimeMessage(session);
      //设置自定义发件人昵称
      String nick = "";
      try {
        nick = javax.mail.internet.MimeUtility.encodeText("昆仔科技");
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
      message.setFrom(new InternetAddress(nick + " <" + sendName + ">"));
      //设置发信人
      // message.setFrom(new InternetAddress(sendName));

      // 邮件的主题
      message.setSubject(subject);
      //收件人
      //message.addRecipient(Message.RecipientType.TO, toAddress);
            /*//抄送人
            Address ccAddress = new InternetAddress("first.lady@whitehouse.gov");
            message.addRecipient(Message.RecipientType.CC, ccAddress);*/

      //---------------------------------------
      //2收件人:可以多个
      //一个的收件人

      if (to == null || to.length <= 0) {
        return false;
      } else if (to.length == 1) {
        Address toAddress = new InternetAddress(to[0]);
        message.setRecipient(Message.RecipientType.TO, toAddress);
        // 邮件的内容
        message.setContent(content, "text/html;charset=" + SysUtilConstant.CHARSET_UTF8);
        // 邮件发送时间
        message.setSentDate(new Date());

        // 第四步：发送邮件
        // 第一个参数：邮件的消息体
        // 第二个参数：邮件的接收人，多个接收人用逗号隔开（test1@163.com,test2@sina.com）
        transport.sendMessage(message, InternetAddress.parse(to[0]));
      } else {
        //多个收件人
        InternetAddress[] receptientsEmail = new InternetAddress[to.length];
        for (int i = 0; i < to.length; i++) {
          receptientsEmail[i] = new InternetAddress(to[i]);
        }
        message.setRecipients(Message.RecipientType.CC, receptientsEmail);
        // 邮件的内容
        message.setContent(content, "text/html;charset=" + SysUtilConstant.CHARSET_UTF8);
        // 邮件发送时间
        message.setSentDate(new Date());

        // 第四步：发送邮件
        Transport.send(message);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }

  public static boolean sendEmail(EmailProperties emailProperties, String[] sendTo, String title, Object message) {
    if (null == emailProperties) {
      emailProperties = BaseEmailConfig.getDefaultEmailConfig();
      log.info("无法加载邮件配置-将使用默认配置");
      return false;
    }
    return send(sendTo, title, message, emailProperties.getProtocol(), emailProperties.getHost(),
        emailProperties.getSendName(), emailProperties.getSendPort(), emailProperties.getUserName(),
        emailProperties.getUserPwd());
  }

  public static boolean sendEmail(String[] sendTo, String title, Object message) {
    final EmailProperties emailProperties = BaseEmailConfig.getDefaultEmailConfig();
    return send(sendTo, title, message, emailProperties.getProtocol(), emailProperties.getHost(),
        emailProperties.getSendName(), emailProperties.getSendPort(), emailProperties.getUserName(),
        emailProperties.getUserPwd());
  }

  public static void main(String[] args) {
    // 您要发送给谁，标题，内容
    //SendMailUtil.send("*****@qq.com", "标题", "内容", "smtp", "smtp.qq.com", "发送人邮箱", "端口号", "邮箱用户名", "邮箱密码");
    SendMailUtil.send(new String[]{"656678879@qq.com"}, "标题", "内容", "smtp", "smtp.sina.com",
        "kunzaikj@sina.com", "465", "kunzaikj", "kunzaikj");
  }
}
